package com.cv.infra.websocket;

import cn.hutool.core.util.ObjUtil;
import com.cv.framework.common.enums.UserTypeEnum;
import com.cv.framework.websocket.core.listener.WebSocketMessageListener;
import com.cv.framework.websocket.core.sender.WebSocketMessageSender;
import com.cv.framework.websocket.core.utils.WebSocketFrameworkUtil;
import com.cv.infra.websocket.message.MessageReceive;
import com.cv.infra.websocket.message.MessageSend;
import com.cv.platform.api.message.CvMessageApi;
import com.cv.platform.api.message.dto.CvMessageDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.Resource;

/**
 * WebSocket 消息监听器
 * 因为 WebSocket 在项目中只用在私信模块，因此 type 可以写死。
 *
 * @author Charles_XDXD
 */
@Component
public class InfraWebSocketMessageListener implements WebSocketMessageListener<MessageSend> {

    @Resource
    private WebSocketMessageSender webSocketMessageSender;

    @DubboReference
    private CvMessageApi cvMessageApi;

    @Override
    public void onMessage(WebSocketSession session, MessageSend message) {
        Long fromUserId = WebSocketFrameworkUtil.getLoginUserId(session);

        if ("message".equals(message.getType())) {
            // 单发
            if (ObjUtil.isNotEmpty(message.getTargetUserId())) {
                MessageReceive messageReceive = new MessageReceive()
                        .setConversationId(message.getConversationId())
                        .setFromUserId(fromUserId)
                        .setContent(message.getContent())
                        .setSingle(true);

                // 调用私信保存 api
                CvMessageDTO cvMessageDTO = new CvMessageDTO()
                        .setConversationId(message.getConversationId())
                        .setSenderId(fromUserId)
                        .setReceiverId(message.getTargetUserId())
                        .setContent(message.getContent());
                cvMessageApi.saveMessage(cvMessageDTO);

                webSocketMessageSender.sendObject(
                        UserTypeEnum.MEMBER.getValue(),
                        message.getTargetUserId(),
                        "infra-message-receive",
                        messageReceive
                );

                webSocketMessageSender.sendObject(
                        UserTypeEnum.ADMIN.getValue(),
                        message.getTargetUserId(),
                        "infra-message-receive",
                        messageReceive
                );
            }
        } else if ("read".equals(message.getType())) {
            cvMessageApi.setRead(message.getConversationId());
            MessageReceive messageReceive = new MessageReceive()
                    .setConversationId(message.getConversationId())
                    .setSingle(true);
            
            // 通知对方已读
            webSocketMessageSender.sendObject(
                    UserTypeEnum.MEMBER.getValue(),
                    "infra-message-read",
                    messageReceive
            );

            webSocketMessageSender.sendObject(
                    UserTypeEnum.ADMIN.getValue(),
                    message.getTargetUserId(),
                    "infra-message-receive",
                    messageReceive
            );
        }

    }

    @Override
    public String getType() {
        return "infra-message-send";
    }

}
